package com.unionftech.content_login.ui.login

import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import com.unionftech.common.R.*
import com.unionftech.common.base.BaseFragment
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.RegexUtils
import com.unionftech.common.utils.autoSize
import com.unionftech.content_login.R
import com.unionftech.common.utils.CheckInputUtil

class LoginCommonFragment : BaseFragment() {
    private val mEtPassword: EditText by lazy { mRootView.findViewById(R.id.et_password) }
    private val mTvGotoForgetPassword: TextView by lazy { mRootView.findViewById(R.id.tv_goto_forget_password) }
    private val mIvClearPassword: ImageView by lazy { mRootView.findViewById(R.id.iv_clear_password) }
    private val mIvShowPassword: ImageView by lazy { mRootView.findViewById(R.id.iv_show_password) }
    private val mBtnLogin: Button by lazy { mRootView.findViewById(R.id.btn_login) }
    private var mCompleteListener: ((result: Boolean) -> Unit)? = null
    private var mPasswordChangeCallback: ((Boolean) -> Unit)? = null
    private var mGotoForgetCallback: (() -> Unit)? = null


    companion object {
        @JvmStatic
        fun newInstance() = LoginCommonFragment().apply {
            arguments = Bundle().apply {}
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun inflaterContentView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_login_common, container, false)
    }

    override fun initLanguageText() {
        super.initLanguageText()
        mEtPassword.hint = DynamicResourceManager.getString(mContext, string.password_hint)
        mTvGotoForgetPassword.text =
            DynamicResourceManager.getString(mContext, string.forget_password)
        mBtnLogin.text = DynamicResourceManager.getString(mContext, string.log_in)

    }

    override fun setView() {
        super.setView()
        hideTitleBar()
        mRootView.setBackgroundColor(resources.getColor(android.R.color.transparent))
    }

    override fun setListener() {
        super.setListener()
        mIvShowPassword.setOnClickListener(this)
        mBtnLogin.setOnClickListener(this)
        mTvGotoForgetPassword.setOnClickListener(this)
        mIvClearPassword.setOnClickListener(this)
        mEtPassword.addTextChangedListener { text ->
            mPasswordChangeCallback?.invoke(!TextUtils.isEmpty(text))
            setClearPassword(text)
        }
    }

    private fun setClearPassword(text: Editable?) {
        if (!TextUtils.isEmpty(text)) {
            mIvClearPassword.visibility = View.VISIBLE
        } else {
            mIvClearPassword.visibility = View.GONE
        }
    }

    private fun hidePasswordText() {
        mIvShowPassword.isSelected = false
        mIvShowPassword.setImageResource(drawable.icon_eye_off)
        mEtPassword.transformationMethod = PasswordTransformationMethod.getInstance()
        mEtPassword.setSelection(mEtPassword.text.length)
    }

    private fun showPasswordText() {
        mIvShowPassword.isSelected = true
        mIvShowPassword.setImageResource(drawable.icon_eye_on)
        mEtPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
        mEtPassword.setSelection(mEtPassword.text.length)
    }

    fun setCompleteListener(listener: (result: Boolean) -> Unit) {
        mCompleteListener = listener
    }

    fun setPasswordChangeCallback(callback: (Boolean) -> Unit) {
        this.mPasswordChangeCallback = callback
    }

    fun setGotoForgetCallback(callback: () -> Unit) {
        this.mGotoForgetCallback = callback
    }

    fun resetPasswordData() {
        mEtPassword.setText("")
        mEtPassword.clearFocus()
        mEtPassword.isSelected = false
    }

    override fun doClick(v: View?) {
        super.doClick(v)
        when (v?.id) {
            R.id.tv_goto_forget_password -> {
                gotoForgetPassword()
            }

            R.id.iv_clear_password -> mEtPassword.setText("")
            R.id.iv_show_password -> {
                if (mIvShowPassword.isSelected) {
                    hidePasswordText()
                } else {
                    showPasswordText()
                }
            }

            R.id.btn_login -> {
                mEtPassword.clearFocus()
                val passwordCheck = checkAndShowForget()
                mCompleteListener?.invoke(passwordCheck)
            }
        }
    }

    private fun checkAndShowForget(): Boolean {
        return !TextUtils.isEmpty(mEtPassword.text)
    }

    private fun gotoForgetPassword() {
        mGotoForgetCallback?.invoke()
    }

    fun getLoginPassword(): String {
        return mEtPassword.text.toString()
    }

    fun setLoginEnable(enable: Boolean) {
        mBtnLogin.isEnabled = enable
    }
}